package com.cybertron.lockdown.spider;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.OOSpider;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.QueueScheduler;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by xiezhouyan on 15-1-20.
 */
public class SpiderRunner {

    private static final SpiderRunner RUNNER = new SpiderRunner();

    private SpiderRunner() {

    }

    public static SpiderRunner create() {
        return RUNNER;
    }

    private static Logger logger = LoggerFactory.getLogger(SpiderBootstrap.class);

    private static final long PERIOD_MILLISECONDS = 24 * 60 * 60 * 1000;

    private final SiteManager siteManager = new SiteManager();
    private final PageProcessor pageProcessor = new LockdownPageProcessor(siteManager);
    private final Pipeline pipeline = new LockdownPipiline(siteManager);

    private final Spider spider = OOSpider.create(pageProcessor).addPipeline(pipeline).thread(20);


    public void run() {
        final String[] urls = siteManager.getSeedUrls();

        Calendar calendar = Calendar.getInstance();
        // 指定01:00:00点执行
        calendar.set(Calendar.HOUR_OF_DAY, 1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        Date date = calendar.getTime();
        Date now = new Date();
        if (date.before(now)) {
            date = DateUtils.addDays(date, 1);
        }

        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                logger.info("execute spider with timer");
                spider.addUrl(urls).setScheduler(new QueueScheduler()).run();
            }
        }, date.getTime() - now.getTime(), PERIOD_MILLISECONDS, TimeUnit.MILLISECONDS);
        spider.addUrl(urls).setScheduler(new QueueScheduler()).runAsync();
    }

    public Spider getSpider() {
        return spider;
    }
}
